/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

@import '../commons/index.css';

:root {
  --spectrum-calendar-border-radius-reset: 0;
  --spectrum-calendar-border-width-reset: 0;
  --spectrum-calendar-margin-y: 24px;
  --spectrum-calendar-margin-x: 32px;
  --spectrum-calendar-max-width: calc((var(--spectrum-calendar-day-width) * 7) + (var(--spectrum-calendar-day-padding) * 12));
  --spectrum-calendar-min-width: calc(var(--spectrum-calendar-day-width) * 7);
  --spectrum-calendar-button-gap: var(--spectrum-global-dimension-size-40);
  --spectrum-calendar-title-margin-x: var(--spectrum-global-dimension-size-75);
}

.spectrum-Calendar {
  display: block;
  isolation: isolate;
}

.spectrum-Calendar--padded {
  margin: var(--spectrum-calendar-margin-x) var(--spectrum-calendar-margin-y);
}

.spectrum-Calendar-header {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  width: 100%;
  gap: var(--spectrum-global-dimension-size-300);
}

.spectrum-Calendar-monthHeader {
  display: grid;
  grid-template-areas: 'prev title next';
  grid-template-columns: minmax(auto, 1fr) auto minmax(auto, 1fr);
  align-content: start;
  width: 100%;
  min-width: var(--spectrum-calendar-min-width);
  max-width: var(--spectrum-calendar-max-width);
  padding: 0 var(--spectrum-calendar-button-gap);
  box-sizing: border-box;
}

.spectrum-Calendar-title {
  font-size: var(--spectrum-calendar-title-text-size);
  font-weight: bold;

  line-height: var(--spectrum-calendar-title-height);
  margin: 0 var(--spectrum-calendar-title-margin-x);
  grid-area: title;

  text-align: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.spectrum-Calendar-prevMonth {
  grid-area: prev;
  justify-self: start;
}

.spectrum-Calendar-nextMonth {
  grid-area: next;
  justify-self: end;
}

.spectrum-Calendar-months {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: var(--spectrum-global-dimension-size-300);
  align-items: start;
}

.spectrum-Calendar-dayOfWeek {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  height: 100%;

  width: 100%;

  border-bottom: none !important; /* override abbr styling from normalize.css */

  font-size: var(--spectrum-calendar-day-title-text-size);
  font-weight: var(--spectrum-calendar-day-title-text-font-weight);
  text-transform: uppercase;
  text-decoration: none !important; /* override abbr styling from normalize.css */

  cursor: default;

  &[title] {
    /* Normalize abbr[title] */
    /* Remove the bottom border in Chrome 57- and Firefox 39-. */
    border-bottom: none;

    /* Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. */
    text-decoration: underline;
    text-decoration: underline dotted;
  }
}

.spectrum-Calendar-body {
  outline: none;
}

.spectrum-Calendar-table {
  table-layout: fixed;
  width: 100%;
  min-width: var(--spectrum-calendar-min-width);
  max-width: var(--spectrum-calendar-max-width);

  border-collapse: collapse;
  border-spacing: 0;

  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.spectrum-Calendar-tableCell {
  text-align: center;
  padding: 0;
  position: relative;
  box-sizing: content-box;
  height: var(--spectrum-calendar-day-height);
  width: var(--spectrum-calendar-day-width);
  padding: var(--spectrum-calendar-day-padding) 0;

  &:focus {
    outline: 0;
  }
}

.spectrum-Calendar-date {
  /* This is fun, but we need to trick the table so it doesn't size itself in a weird way */
  position: absolute;
  display: block;
  top: 0;
  left: 0;

  box-sizing: border-box;

  height: var(--spectrum-calendar-day-height);
  width: 100%;
  margin: var(--spectrum-calendar-day-padding) 0;

  border-radius: var(--spectrum-calendar-day-width);

  font-size: var(--spectrum-calendar-day-text-size);
  line-height: var(--spectrum-calendar-day-height);
  white-space: nowrap;

  cursor: default;

  /* compute the "auto" margin to center the date text manually rather than using the keyword to ensure consistent rounding. */
  --margin: calc((100% - var(--spectrum-calendar-day-width)) / 2);

  .spectrum-Calendar-dateText {
    display: block;
    width: var(--spectrum-calendar-day-width);
    margin: 0 auto;
    margin-inline-start: auto;
    margin-inline-end: var(--margin);
  }

  &:focus {
    outline: 0;
  }

  &:lang(ja), &:lang(zh), &:lang(ko) {
    font-size: var(--spectrum-calendar-day-text-size-han);
  }

  &.is-disabled {
    cursor: default;
    pointer-events: none;
  }

  &.is-unavailable {
    .spectrum-Calendar-dateText span {
      position: relative;

      &:after {
        content: '';
        position: absolute;
        top: 50%;
        left: -4px;
        right: -4px;
        height: 2px;
        transform: rotate(-16deg);
        border-radius: 1px;
        background: currentColor;
      }
    }
  }

  &.is-outsideMonth {
    visibility: hidden;
  }

  /* focus ring */
  &:before {
    border-radius: calc(var(--spectrum-calendar-day-width) + var(--spectrum-alias-focus-ring-gap));
    content: '';
    display: block;
    position: absolute;
    top: calc(50% - calc(var(--spectrum-calendar-day-width) / 2));
    left: calc(50% - calc(var(--spectrum-calendar-day-width) / 2));
    width: calc(var(--spectrum-calendar-day-width) + var(--spectrum-alias-focus-ring-gap) * 2);
    height: calc(var(--spectrum-calendar-day-height) + var(--spectrum-alias-focus-ring-gap) * 2);
    margin: 0;
    transform: translateX(0); /* Forces FF to render on the pixel grid */

    transition: box-shadow var(--spectrum-global-animation-duration-100) ease-out,
                margin var(--spectrum-global-animation-duration-100) ease-out;
  }

  &.is-focused {
    &:before {
      margin: calc(var(--spectrum-alias-focus-ring-gap) * -1);
    }
  }

  /* hover and selected states */
  &:after {
    position: absolute;
    top: calc(50% - calc(var(--spectrum-calendar-day-width) / 2));
    left: calc(50% - calc(var(--spectrum-calendar-day-width) / 2));

    display: block;

    height: var(--spectrum-calendar-day-height);
    width: var(--spectrum-calendar-day-width);

    border-radius: var(--spectrum-calendar-day-width);

    content: '';
    z-index: -1;
  }

  &.is-range-selection {
    /* overrides to make the cells appear connected */
    margin: var(--spectrum-calendar-day-padding) 0;
    border-width: var(--spectrum-calendar-border-width-reset);
    line-height: var(--spectrum-calendar-day-height);
    border-radius: var(--spectrum-calendar-border-radius-reset);

    &.is-range-start,
    &.is-selection-start {
      border-start-start-radius: var(--spectrum-calendar-day-width);
      border-end-start-radius: var(--spectrum-calendar-day-width);
      /* reduce the width by the "auto" margin and end align so the background doesn't extend to the left */
      inset-inline-start: auto;
      inset-inline-end: 0;
      width: calc(100% - var(--margin));

      &:before,
      &:after,
      .spectrum-Calendar-dateText {
        inset-inline-start: 0;
      }

      &:after,
      .spectrum-Calendar-dateText {
        margin-inline-start: 0;
      }
    }

    &.is-range-end,
    &.is-selection-end {
      border-start-end-radius: var(--spectrum-calendar-day-width);
      border-end-end-radius: var(--spectrum-calendar-day-width);
      /* reduce the width by the "auto" margin and start align so the background doesn't extend to the right */
      /* this must be measured from the right rather than using width to avoid rounding errors in Safari */
      width: auto;
      inset-inline-start: 0;
      inset-inline-end: var(--margin);

      &:before,
      &:after,
      .spectrum-Calendar-dateText {
        inset-inline-start: auto;
        inset-inline-end: 0;
      }

      &:after,
      .spectrum-Calendar-dateText {
        margin-inline-end: 0;
      }
    }

    &.is-selection-start.is-selection-end,
    &.is-selection-start.is-range-end,
    &.is-selection-end.is-range-start,
    &.is-range-start.is-range-end {
      width: var(--spectrum-calendar-day-width);
      inset-inline-start: 0;
      inset-inline-end: 0;
      margin-inline-start: auto;
      margin-inline-end: var(--margin);
      border-radius: var(--spectrum-calendar-day-width);

      &:after,
      .spectrum-Calendar-dateText {
        margin-inline-start: auto;
        margin-inline-end: var(--margin);
      }
    }
  }
}
